/*
 * Filename:    AuditDispatchController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014-11-10
 */
package com.paic.mhis.hcpms.audit.web.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HttpProcessUtil;
import com.paic.mhis.hcpms.audit.biz.service.AuditOpinionService;
import com.paic.mhis.hcpms.audit.constants.AuditConstants;
import com.paic.mhis.hcpms.audit.dto.AuditOpinionDTO;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

/**
 * 该控制器主要是对经办岗调查结论录入
 * 
 * @author EX-LUORONG001
 * 
 */
@Controller
@RequestMapping("/auditOpinion/")
public class AuditOpinionController {

	@BizServicesProxy(beanName = "auditOpinionServiceImpl")
	private AuditOpinionService auditOpinionService;

	/**
	 * 根据事件编码加载对应的意见（初审，审核）
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("loadOpinionList.do")
	@ResponseBody
	public Map<String, Object> loadOpinionList(@ModelAttribute
	AuditOpinionDTO opinionDTO, HttpServletRequest request) {

		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> opinion = this.auditOpinionService.getOpinionList(
				opinionDTO, AuditConstants.opinionType_1,
				AuditConstants.opinionType_2);
		result.put("result", opinion);
		return result;
	}

	/**
	 * 根据事件编码加载对应的意见（录入、复核）
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("loadOpinionRiewList.do")
	@ResponseBody
	public Map<String, Object> loadOpinionRiewList(@ModelAttribute
	AuditOpinionDTO opinionDTO, HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> opinion = this.auditOpinionService.getOpinionList(
				opinionDTO, AuditConstants.opinionType_3,
				AuditConstants.opinionType_4);
		result.put("result", opinion);
		return result;
	}

	/**
	 * 根据事件编码加载对应的意见（录入、复核、审核）
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("queryOpinionList.do")
	@ResponseBody
	public Map<String, Object> queryOpinionList(@ModelAttribute
	AuditOpinionDTO opinionDTO, HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> opinion = this.auditOpinionService.getOpinionList(
				opinionDTO, AuditConstants.opinionType_3,
				AuditConstants.opinionType_4);
		AuditOpinionDTO auditOpinionDTO = this.auditOpinionService
				.getAuditOpinion(opinionDTO);
		opinion.put("audit", auditOpinionDTO);
		result.put("result", opinion);
		return result;
	}

	/**
	 * 根据事件编码加载对应的意见（事件调查）
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("loadAuditOpinion.do")
	@ResponseBody
	public Map<String, Object> loadAuditOpinion(@ModelAttribute
	AuditOpinionDTO opinionDTO, HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		opinionDTO.setOpinionType(AuditConstants.opinionType_3);
		opinionDTO.setTaskType("1");
		AuditOpinionDTO auditOpinionDTO = this.auditOpinionService
				.queryAuditOpinionDTO(opinionDTO);
		result.put("result", auditOpinionDTO);
		return result;
	}

	/**
	 * 根据案件编码加载对应的意见（案件复核）
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("loadAuditCaseOpinion.do")
	@ResponseBody
	public Map<String, Object> loadAuditCaseOpinion(@ModelAttribute
	AuditOpinionDTO opinionDTO, HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		opinionDTO.setOpinionType(AuditConstants.opinionType_12);
		opinionDTO.setTaskType("2");
		AuditOpinionDTO auditOpinionDTO = this.auditOpinionService
				.queryAuditOpinionDTO(opinionDTO);
		result.put("result", auditOpinionDTO);
		return result;
	}

	/**
	 * 根据事件编码加载对应的意见（录入、复核、稽核）[调查回退]
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("loadOpinionProjectList.do")
	@ResponseBody
	public Map<String, Object> loadOpinionProjectList(@ModelAttribute
	AuditOpinionDTO opinionDTO, HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		String userName = user.getUsername();
		Map<String, Object> opinion = this.auditOpinionService
				.loadOpinionProjectList(opinionDTO,
						AuditConstants.opinionType_3,
						AuditConstants.opinionType_4, "0", "0", userName, false);
		result.put("result", opinion);
		return result;

	}

	/**
	 * 根据事件编码加载对应的意见（录入、复核、稽核）[复核回退]
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("loadOpinionProjects.do")
	@ResponseBody
	public Map<String, Object> loadOpinionProjects(@ModelAttribute
	AuditOpinionDTO opinionDTO, HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		String userName = user.getUsername();
		Map<String, Object> opinion = this.auditOpinionService
				.loadOpinionProjectList(opinionDTO,
						AuditConstants.opinionType_3,
						AuditConstants.opinionType_4, "1", "0", userName, false);
		result.put("result", opinion);
		return result;

	}

	/**
	 * 根据案件编码加载对应的意见（审核、复核、稽核）
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("loadOpinionCaseList.do")
	@ResponseBody
	public Map<String, Object> loadOpinionCaseList(@ModelAttribute
	AuditOpinionDTO opinionDTO, HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> opinion = this.auditOpinionService.getOpinionList(
				opinionDTO, AuditConstants.opinionType_12,
				AuditConstants.opinionType_13);
		opinionDTO.setOpinionType(AuditConstants.opinionType_11);
		opinionDTO.setTaskType("2");
		AuditOpinionDTO auditOpinionDTO = this.auditOpinionService
				.queryAuditOpinionDTO(opinionDTO);
		opinion.put("audit", auditOpinionDTO);
		result.put("result", opinion);
		return result;

	}

	/**
	 * 根据案件编码加载对应的意见（审核、复核、稽核）[回退]
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("loadOpinionCases.do")
	@ResponseBody
	public Map<String, Object> loadOpinionCases(@ModelAttribute
	AuditOpinionDTO opinionDTO, HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		String userName = user.getUsername();
		Map<String, Object> opinion = this.auditOpinionService
				.loadOpinionProjectList(opinionDTO,
						AuditConstants.opinionType_11,
						AuditConstants.opinionType_12, "0", "1", userName,
						false);
		result.put("result", opinion);
		return result;

	}

	/**
	 * 根据案件编码加载对应的意见（审核、复核、稽核）[回退]
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("loadOpinionLists.do")
	@ResponseBody
	public Map<String, Object> loadOpinionLists(@ModelAttribute
	AuditOpinionDTO opinionDTO, HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		String userName = user.getUsername();
		Map<String, Object> opinion = this.auditOpinionService
				.loadOpinionProjectList(opinionDTO,
						AuditConstants.opinionType_11,
						AuditConstants.opinionType_12, "1", "1", userName,
						false);
		result.put("result", opinion);
		return result;

	}

	/**
	 * 查询所有意见信息
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("queryAllOpinion.do")
	@ResponseBody
	public List<AuditOpinionDTO> queryAllOpinion(@ModelAttribute
	AuditOpinionDTO opinionDTO, HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		String caseCode = request.getParameter("caseCode");
		String problemNo = request.getParameter("problemNo");
		return auditOpinionService.queryAllOpinion(opinionDTO, user, caseCode,
				problemNo);

	}

	/**
	 * 新增意见同时更新当前事件的状态
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addOpinion.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> auditConfirmProcess(HttpServletRequest request)
			throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);

			AuditOpinionDTO opinionDTO = JSONObject.toJavaObject(transferJson,
					AuditOpinionDTO.class);

			opinionDTO.setDealUserName(user.getUsername());
			opinionDTO.setOpinionType(AuditConstants.opinionType_3);
			opinionDTO.setTaskType(AuditConstants.taskType_01);

			result = this.auditOpinionService.processProblemAudit(opinionDTO,
					transferJson, user.getUsername(), user, result);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 新增意见
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addReviewOpinion.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addReviewOpinion(HttpServletRequest request)
			throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);

			AuditOpinionDTO opinionDTO = JSONObject.toJavaObject(transferJson,
					AuditOpinionDTO.class);
			opinionDTO.setDealUserName(user.getUsername());
			opinionDTO.setOpinionType(AuditConstants.opinionType_4);
			opinionDTO.setTaskType(AuditConstants.taskType_01);

			result = this.auditOpinionService.processBackProblemReview(
					opinionDTO, transferJson, user.getUsername(), user, result);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 新增意见(修改稽核结论、添加稽核结论复核)（回退案件复核）
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updateReviewOpinion.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateReviewOpinion(HttpServletRequest request)
			throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);

			AuditOpinionDTO opinionDTO = JSONObject.toJavaObject(transferJson,
					AuditOpinionDTO.class);
			opinionDTO.setDealUserName(user.getUsername());
			opinionDTO.setOpinionType(AuditConstants.opinionType_12);
			opinionDTO.setTaskType(AuditConstants.taskType_02);

			result = this.auditOpinionService.processCaseReview(opinionDTO,
					transferJson, user, result);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 新增意见(修改稽核结论、添加稽核结论复核)（回退案件复核）（批量提交复核）
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("updateBatchReviewOpinion.do")
	@ResponseBody
	public Map<String, Object> updateBatchReviewOpinion(@ModelAttribute
	AuditOpinionDTO opinionDTO, HttpServletRequest request) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			result = this.auditOpinionService.updateBatchCase(opinionDTO, user,
					result);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 根据事件编码加载对应的意见
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("loadOpinion.do")
	@ResponseBody
	public Map<String, Object> loadOpinion(@ModelAttribute
	AuditOpinionDTO opinionDTO, HttpServletRequest request) {

		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> opinion = this.auditOpinionService
				.getOpinion(opinionDTO);
		result.put("result", opinion);
		return result;

	}

	@RequestMapping("approvementAuditOpinion.do")
	@ResponseBody
	public Map<String, Object> approvementAuditOpinion(@ModelAttribute
	AuditOpinionDTO opinionDTO, HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		opinionDTO.setDealUserName(user.getUsername());
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			result = this.auditOpinionService.approvementAuditOpinion(
					opinionDTO, user, result);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;

	}

	@RequestMapping("approvementAudits.do")
	@ResponseBody
	public Map<String, Object> approvementAudits(@ModelAttribute
	AuditOpinionDTO opinionDTO, HttpServletRequest request) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		opinionDTO.setDealUserName(user.getUsername());
		String tAry = request.getParameter("caseCodes");
		String[] caseCodes = tAry.split(",");
		try {
			result = this.auditOpinionService.approvementAudits(opinionDTO,
					user, result, caseCodes);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;

	}

	@RequestMapping(value = "approvementProblemOpinion.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> approvementProblemOpinion(
			HttpServletRequest request) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);

			AuditOpinionDTO opinionDTO = JSONObject.toJavaObject(transferJson,
					AuditOpinionDTO.class);

			opinionDTO.setDealUserName(user.getUsername());
			result = this.auditOpinionService.approvementProblemOpinion(
					opinionDTO, transferJson, user, result);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 批量操作（调查）
	 * 
	 * @param vo
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws Exception
	 */
	@RequestMapping(value = "onBatchOperateDc.do", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> onBatchOperateDc(HttpServletRequest request)
			throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();

		try {
			String str[] = request.getParameterValues("problemNos");
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");
			result = auditOpinionService
					.onBatchOperateDcServ(str, user, result);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 批量操作（复核）
	 * 
	 * @param vo
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws Exception
	 */
	@RequestMapping(value = "onBatchOperateReview.do", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> onBatchOperateReview(HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			String str[] = request.getParameterValues("problemNos");
			String opinionDesc = request.getParameter("opinionDesc");
			String opinionState = request.getParameter("opinionState");
			UserDetails user = (UserDetails) request.getSession().getAttribute(
					"user");

			AuditOpinionDTO opinionDTO = new AuditOpinionDTO();
			opinionDTO.setOpinionDesc(opinionDesc);
			opinionDTO.setOpinionState(opinionState);
			opinionDTO.setOnlySave("0");
			opinionDTO.setDealUserName(user.getUsername());
			opinionDTO.setOpinionType(AuditConstants.opinionType_4);
			opinionDTO.setTaskType(AuditConstants.taskType_01);

			result = auditOpinionService.onBatchOperateReviewServ(str,
					opinionDTO, user, result);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}
	/**
	 * 流程流转情况详情展示（事件）
	 * 
	 * @param vo
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws Exception
	 */
	@RequestMapping(value = "showWFHistory.do", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> showWFHistory(HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			String problemNo = request.getParameter("problemNo");
			result = auditOpinionService.showWFHistory(problemNo, result);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}
	/**
	 * 流程流转情况详情展示（案件）
	 * 
	 * @param vo
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws Exception
	 */
	@RequestMapping(value = "showWFCaseHistory.do", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> showWFCaseHistory(HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			String caseCode = request.getParameter("caseCode");
			String relatedCode = request.getParameter("relatedCode");
			result = auditOpinionService.showWFCaseHistory(caseCode,relatedCode, result);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}
}
